angular.module('starter.services', [])

/**
 * A simple example service that returns some data.
 */
.factory('Items', function($http,$window) {
  // Might use a resource here that returns a JSON array
    var accessToken = $window.sessionStorage['accessToken'];
  // Some fake testing data
  return {
    all: function($scope) {///js/item.json
        var items = [];
        //alert(items[0].itemName);
        $http.get('http://192.168.199.165:3000/item?accessToken='+ accessToken).success(function(data){
            items = data;
            //alert(items[0].itemName)
            $scope.items =  items.program;
        });


    },
    get: function(itemsId) {
      // Simple index lookup
      return items[itemsId];
    },
      getDeviceList: function($scope,itemsId) {///js/device.json
          //var deviceLista = [];
          // Simple index lookup
          var device = function(did,deviceName,online,deviceState,timerProgram){
              this.did=did;
              this.deviceName=deviceName;
              this.online=online;
              this.deviceState=deviceState;
              this.timerProgram=timerProgram;
          }
          $http.get('http://192.168.199.165:3000/device?accessToken='+ accessToken).success(function(data){
              for(var i=0;i<data.length;i++){
                  /*if(data[i].itemID==itemsId){
                      $scope.deviceList = data[i].device;
                  }*/
                  var scene = data.scenes[i];
                   var device = new device(scene.sceneId,scene.title,"0");
              }

          });
      },
      getDeviceDetail: function($scope,did) {///js/deviceDetail.json
          $http.get('http://192.168.199.165:3000/deviceDetail').success(function(data){
              for(var i=0;i<data.length;i++){
                  if(data[i].did==did){
                      $scope.deviceDetail = data[i].deviceDetail[0];
                  }
              }

          });
        //return (items[itemsId].item)[deviceID];
    },
      getSchemeList: function($scope) {
          $http.get('http://192.168.199.165:3000/scheme').success(function(data){
              for(var i=0;i<data.length;i++){
                  data[i].num=i+1;
                  if(data[i].selected){
                      $scope.select = {tid : data[i].tid};
                  }
              }
              $scope.schemeList = data;

          });
      }
  }
})

    .factory('Relay',function($http){

        var rList = {"did": 0, "relayList":
                [
                    {"rid":0,"relayName":"继电器1","relayState":"0","timerProgram":"每天24：00关，晚7点开"},
                    {"rid":1,"relayName":"继电器2","relayState":"1","timerProgram":"每天06：00关，晚8点开"},
                    {"rid":2,"relayName":"继电器3","relayState":"1","timerProgram":"每天23：00关，晚8点开"},
                    {"rid":3,"relayName":"继电器4","relayState":"0","timerProgram":"每天24：00关，晚7点开"},
                    {"rid":4,"relayName":"继电器5","relayState":"0","timerProgram":"每天05：00关，晚5点开"},
                    {"rid":5,"relayName":"继电器6","relayState":"0","timerProgram":"每天05：00关，晚5点开"}
                ]
            }


       return {
           getRelayList: function($scope,did) {
               $http.get('http://192.168.199.165:3000/relay').success(function(data){
                   for(var i=0;i<data.length;i++){
                       if(data[i].did==did){
                           $scope.relayList = data[i].relayList;
                       }
                   }

               });
           },
           getRelayMoreList: function($scope) {
               //


               $http.get('http://192.168.199.165:3000/relay').success(function(data){
                   for(var i=0;i<data.length;i++){
                       if(data[i].did==0){

                           for(var j=0;j<data[i].relayList.length;j++){
                               $scope.relayList.push(data[i].relayList[j]);
                           }
                       }
                   }
                   $scope.$broadcast('scroll.infiniteScrollComplete');

               });

           }
       }
    })

.factory('Devices', function(){
    var devices = [
            { id: 0, name: '项目1',item:{id: 0, name: '设备1'} },
            { id: 1, name: '项目2',item:{id: 0, name: '设备2'} },
            { id: 2, name: '项目3',item:{id: 0, name: '设备3'} },
            { id: 3, name: '项目4',item:{id: 0, name: '设备4'} }
    ];
    return {
        all: function() {
            return devices;
        },
        get: function(devicesId) {
            // Simple index lookup
            return devices[devicesId];
        }
    }
})

.factory('LoginService',function($http,$window){//用户登录
        /*function login(loginData,$state) {
            //alert(loginData.userName+"---"+loginData.password);
            $http.post('http://192.168.199.165:3000/login',{userName:loginData.userName,password:loginData.password})
            //$http.post('http://api.loiot.com/oauth2/assessTokenLogin?data={"nm":loginData.userName,"pwd":loginData.password,"clientId":"88"}')
                .success(function(result){
                   // alert(result);
                    $window.sessionStorage['loginSuccess'] = result;
                    $state.go('tab.items');
                   // return 'true';
                });
        }*/
        return {
            login: function(loginData,$state,$scope,$ionicPopup){
                //alert(loginData.userName+"---"+loginData.password);
                $http.post('http://192.168.199.165:3000/login?userName='+loginData.userName+'&password='+loginData.password+'&clientId='+loginData.clientId)
                    //$http.post('http://api.loiot.com/oauth2/assessTokenLogin?data={"nm":"'+loginData.userName+'","pwd":"'+loginData.password+'","clientId":"'+loginData.clientId+'"}')
                    .success(function(result){
                        // alert(result);
                            $scope.showAlert = function(msg) {
                                var alertPopup = $ionicPopup.alert({
                                    title: '提示',
                                    template: msg,
                                    okType:'button1'
                                });
                                alertPopup.then(function(res) {
                                    console.log('Thank you for not eating my delicious ice cream cone');
                                });
                            };

                        var st = result.ra.st;
                        switch (st)
                        {
                            case 1:
                                $window.sessionStorage['accessToken'] = result.data.accessToken;//登录成功后存储accessToken
                                $state.go('tab.items');
                                //$state.go('test');
                                // return 'true';
                                break;
                            case -1:
                                //$scope.status = '提示：登录名不能为空！';
                                $scope.showAlert('用户名不能为空！');
                                break;
                            case -2:
                                //$scope.status = '提示：密码不能为空！';
                                $scope.showAlert('密码不能为空！');
                                break;
                            case -3:
                                //$scope.status = '提示：客户端ID不能为空！';
                                $scope.showAlert('客户端ID不能为空！');
                                break;
                            case -4:
                                //$scope.status = '提示：登录名不存在！';
                                $scope.showAlert('用户名不存在！');
                                break;
                            case -5:
                                //$scope.status = '提示：密码不正确！';
                                $scope.showAlert('密码不正确！');
                                break;
                            case -6:
                                //$scope.status = '提示：客户端ID不存在！';
                                $scope.showAlert('客户端ID不存在！');
                                break;
                            default:
                                //$scope.status = '提示：未知错误！';
                                $scope.showAlert('未知错误！');
                                break;
                        }

                    });
            }
        }
    });